<?php

require FCPATH . '/application/controllers/backend/backend.php';

class Employee extends Backend {

	public function __construct() {
		parent::__construct();
		$this->load->model('employee_model', 'model');
	}

	public function index() {
		$data = array(
			'l_position' => array(array('name' => '员工资料管理'))
		);

		$this->load_js(array('jquery-ui-1.8.20.custom.min', 'ImageBrowser', 'backend_employee'));
		$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom'));

		$this->rend('employee', $data);
	}

	public function records() {
		$page = intval($this->input->get('page'));
		$page_size = intval($this->input->get('pageSize'));
		$page = $page ? $page : 1;
		$page_size = $page_size ? $page_size : 10;
		$offset = $page_size * ($page - 1);

		$conditions = array();
		$id = intval($this->input->get('id'));
		if (!empty($id)) {
			$conditions[] = array('where', 'id', $id);
		}

		$name = trim($this->input->get('name'));
		if (!empty($name)) {
			$conditions[] = array('or_like', 'name_zh', $name);
			$conditions[] = array('or_like', 'name_fr', $name);
			$conditions[] = array('or_like', 'name_en', $name);
		}

		$image = intval($this->input->get('image'));
		if (!empty($image)) {
			$conditions[] = array('where', 'image', $image);
		}

		$title = trim($this->input->get('title'));
		if (!empty($title)) {
			$conditions[] = array('or_like', 'title_zh', $title);
			$conditions[] = array('or_like', 'title_fr', $title);
			$conditions[] = array('or_like', 'title_en', $title);
		}

		$start = $this->input->get('start');
		if (!empty($start)) {
			$conditions[] = array('where', 'created_at >=', $start);
		}

		$end = $this->input->get('end');
		if (!empty($end)) {
			$conditions[] = array('where', 'created_at <=', $end);
		}

		$order_fields = array('name_zh','name_fr','name_en', 'title_zh', 'title_fr', 'title_en', 'created_at');
		$order_field = intval($this->input->get('order_field'));
		$order_type = intval($this->input->get('order_type'));
		$orders = array();
		if ($order_field >= 0 && $order_field < count($order_fields)) {
			$orders = array(
				array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
			);
		}
		$this->json_back('XT001', '', array(
			'total' => $this->model->total($conditions),
			'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
		));
	}

	public function insert() {
		$data = array(
			'name_zh' => trim($this->input->post('name_zh')),
			'name_fr' => trim($this->input->post('name_fr')),
			'name_en' => trim($this->input->post('name_en')),
			'image' => intval($this->input->post('image')),
			'title_zh' => trim($this->input->post('title_zh')),
			'title_fr' => trim($this->input->post('title_fr')),
			'title_en' => trim($this->input->post('title_en')),
		);
		if (($id = $this->model->insert($data)) > 0) {
			$this->json_back('XT001', '', array('id' => $id));
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function update() {
		$id = intval($this->input->post('id'));
		$data = array(
			'name_zh' => trim($this->input->post('name_zh')),
			'name_fr' => trim($this->input->post('name_fr')),
			'name_en' => trim($this->input->post('name_en')),
			'image' => intval($this->input->post('image')),
			'title_zh' => trim($this->input->post('title_zh')),
			'title_fr' => trim($this->input->post('title_fr')),
			'title_en' => trim($this->input->post('title_en')),
		);
		if ($this->model->update($id, $data)) {
			$this->json_back('XT001', '', array('id' => $id));
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function delete() {
		$id = intval($this->input->get('id'));
		if ($this->model->delete($id)) {
			$this->json_back('XT001', '删除成功。');
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

}
